﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using virtuaPol.Main;
using virtuaPol.Model;

namespace virtuaPol.Net
{
    /// <summary>
    /// Interaction logic for NewGameDlg.xaml
    /// </summary>
    public partial class NewGameDlg : Window
    {
        private const String TEMPLATE_WORLD_FILE = "world.vpw";

        private CWorld world;
        public CWorld World { get { return world; } set { world = value; } }

        public bool IsCanceled { get; set; }

        public NewGameDlg()
        {
            IsCanceled = true;
            InitializeComponent();
            loadWorldScenario();
            CountriesList.ItemsSource = filterUnplayableCountries(World.Countries);
        }

        private IEnumerable<String> filterUnplayableCountries(IList<CCountry> countries)
        {
            List<String> playable = new List<String>();

            foreach (CCountry c in countries)
            {
                if (c.Playable) playable.Add(c.Name);
            }

            return playable;
        }

        private void loadWorldScenario()
        {
            bool res = WorldLoader.load(out world, TEMPLATE_WORLD_FILE);
        }

        private void BtCancel_Click(object sender, RoutedEventArgs e)
        {
            IsCanceled = true;
            this.Close();
        }

        private void BtStartGame_Click(object sender, RoutedEventArgs e)
        {
            if (CountriesList.SelectedItem == null)
                MessageBox.Show("You have to select a Country!");
            else
            {
                // Set Played country in world object and initialize world
                World.PlayerCountryKey = (string)CountriesList.SelectedItem;
                IsCanceled = false;
                this.Close();
            }
        }
    }
}
